package org.manon.json.asm.ser;

import static org.manon.json.asm.ASMUtil.TYPE_FILTER;
import static org.manon.json.asm.ASMUtil.TYPE_OBJECT;
import static org.manon.json.asm.ASMUtil.TYPE_OUTPUTSTREAM;
import static org.manon.json.asm.ASMUtil.TYPE_VOID;

import java.io.IOException;

import org.manon.json.asm.ASMContextSerializer;
import org.manon.json.asm.MethodVisitorTemplate;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

public final class CreateMethodObjectTemplate extends MethodVisitorTemplate<ASMContextSerializer> {
	
	public CreateMethodObjectTemplate() {
		super(Opcodes.ACC_PUBLIC,
				"serialize",
				Type.getMethodDescriptor(TYPE_VOID, TYPE_OUTPUTSTREAM, TYPE_OBJECT,TYPE_FILTER),
				null,
				new String[]{Type.getInternalName(IOException.class)});
	}

	/**
	 * @see org.manon.json.asm.MethodVisitorTemplate#doCreate(org.manon.json.asm.ASMContextSerializer, org.objectweb.asm.MethodVisitor)
	 */
	@Override
	protected void doCreate(ASMContextSerializer context, MethodVisitor methodVisitor) throws Exception {
		methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
		methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
		methodVisitor.visitVarInsn(Opcodes.ALOAD, 2);
		methodVisitor.visitTypeInsn(Opcodes.CHECKCAST,Type.getInternalName(context.getEntity().getEntityClass()));
		methodVisitor.visitVarInsn(Opcodes.ALOAD, 3);
		methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL,context.getClassName(),"serialize", 
				Type.getMethodDescriptor(TYPE_VOID, TYPE_OUTPUTSTREAM, Type.getType(Type.getDescriptor(context.getEntity().getEntityClass())),TYPE_FILTER));
	}
	
}